﻿
Imports System.Net
Imports std

Public Class IPAddress2
    Inherits IPAddress
    Implements ICloneable, IComparable, IComparable(Of IPAddress), IComparable(Of IPAddress2)

    Public Sub New(ByVal address() As Byte)
        MyBase.New(address)
    End Sub

    Public Sub New(ByVal address As Int64)
        MyBase.New(address)
    End Sub

    Public Sub New(ByVal address() As Byte, ByVal scopeId As Int64)
        MyBase.New(address, scopeId)
    End Sub

    Public Sub New(ByVal address As IPAddress)
        MyBase.New(address.GetAddressBytes())
    End Sub

    Public Shared Function create(ByVal address As String) As IPAddress
        Dim tmp As vector(Of IPAddress) = Nothing
        tmp = dnscache.resolve(address)
        If Not tmp Is Nothing AndAlso tmp.size() > 0 Then
            Return tmp(0)
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function createV4(ByVal address As String) As IPAddress
        Dim tmp As vector(Of IPAddress) = Nothing
        tmp = ipv4dnscache.resolve(address)
        If Not tmp Is Nothing AndAlso tmp.size() > 0 Then
            Return tmp(0)
        Else
            Return Nothing
        End If
    End Function

    Public Function Clone() As Object Implements ICloneable.Clone
        Return New IPAddress2(Me)
    End Function

    Public Function CompareTo(ByVal obj As Object) As Int32 Implements IComparable.CompareTo
        Return CompareTo(convertor.cast(Of IPAddress)(obj, False))
    End Function

    Public Function CompareTo(ByVal other As IPAddress2) As Int32 Implements IComparable(Of IPAddress2).CompareTo
        Return CompareTo(convertor.cast(Of IPAddress)(other, False))
    End Function

    Public Shared Function compare(ByVal this As IPAddress, ByVal that As IPAddress) As Int32
        If this Is Nothing AndAlso that Is Nothing Then
            Return 0
        ElseIf this Is Nothing Then
            Return -1
        ElseIf that Is Nothing Then
            Return 1
        Else
            Return memcmp(this.GetAddressBytes(), that.GetAddressBytes())
        End If
    End Function

    Public Function CompareTo(ByVal other As IPAddress) As Int32 Implements IComparable(Of IPAddress).CompareTo
        Return compare(Me, other)
    End Function
End Class
